import rosbag
import numpy as np
import sensor_msgs.point_cloud2 as pc2
import open3d as o3d
import cv2
import tqdm
from cv_bridge import CvBridge
from tictoc import TicToc
import csv
import os
import json
import argparse
import multiprocessing
from utils import topic2path, get_ms
from my_io import write_points, copy_file, files_filter


def main_worker(data_path, args):
    ori_dir = os.path.join(data_path, 'camera', 'camera82')
    new_dir = os.path.join(data_path, 'camera', 'camera81')

    def rename_folder(old_folder_name, new_folder_name):
        try:
            # 使用 os.rename() 方法重命名文件夹
            os.rename(old_folder_name, new_folder_name)
            print(f'文件夹已从 "{old_folder_name}" 重命名为 "{new_folder_name}"')
        except FileNotFoundError:
            print(f'文件夹 "{old_folder_name}" 未找到')
        except PermissionError:
            print('没有权限重命名此文件夹')
        except Exception as e:
            print(f'重命名时发生错误: {e}')

    rename_folder(ori_dir, new_dir)


def main(args):
    # 统计耗时
    cost = TicToc("相机文件夹重命名")
    assert os.path.exists(args.data_path)
    frames = os.listdir(args.data_path)
    frames.sort(key=lambda x: x)
    files = []
    for dir in frames:
        dir = os.path.join(args.data_path, dir)
        if os.path.isdir(dir):
            files.append(dir)

    process_size = len(files)
    manager = multiprocessing.Manager()
    if process_size > 1:
        pool = multiprocessing.Pool(process_size)
        counter_list = manager.list()
        for idx in range(process_size):
            pool.apply_async(main_worker, args=(files[idx], args))
        pool.close()
        pool.join()
    else:
        main_worker(files[0], args)

    print("---------------------------------------------------------")
    print("处理完成: {}".format(files))
    cost.toc()
    print("---------------------------------------------------------")


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Configuration Parameters')
    parser.add_argument('--data-path',
                        default='/media/adt/ZWH4T/ZWH/Dataset3d/dataset_18xx-bag/cyw2.0/calib/datasets')
    args = parser.parse_args()

    main(args)